package com.fire.user.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fire.core.base.controller.BaseController;
import com.fire.core.mode.TMitUserToken;
import com.fire.core.service.ITMitServiceDicBaseServices;
import com.fire.core.util.BlogUtil;
import com.fire.core.util.ToolUtil;
import com.fire.user.constants.ErrorCodeConstants;
import com.fire.user.model.ServiceCode;
import com.fire.user.model.TMitChannel;
import com.fire.user.service.IServiceCodeService;
import com.fire.user.service.ITMitChannelService;
import com.fire.user.service.IUaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class UaController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UaController.class);

	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private IUaService uaService;
	@Autowired
	private IServiceCodeService serviceCodeService;
	@Autowired
	private ITMitServiceDicBaseServices baseServices;
	@Autowired
    private ITMitChannelService channelService;

	/**
	 * 统一认证登陆
	 * 
	 * @param request
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/ualogin", method = RequestMethod.POST)
	@ResponseBody
	public Object unifiedAuthentication(HttpServletRequest request, @RequestBody String json) {
        BlogUtil.inlog(json,"","mit/user/ualogin","unifiedAuthentication","req");
        Map map = json2Map(json);
        Map ajaxResult = new HashMap();

        String URL = baseServices.getBaseByBkey("GUANGDONG_YBTURL");

        String uaencryptsc = String.valueOf(map.get("uaencryptsc"));
        EntityWrapper<ServiceCode> scwrapper = new EntityWrapper<>();
        scwrapper.eq("uaencryptsc",uaencryptsc);
        ServiceCode serviceCode = serviceCodeService.selectOne(scwrapper);
        if (ToolUtil.isEmpty(serviceCode)){
            LOGGER.error("UaController.unifiedAuthentication().debug  错误为：uaencryptsc 效验不合法 返回首页URL");
            ajaxResult.put("status", ErrorCodeConstants.USER_MOBLIE_ERROR_ERL);
            ajaxResult.put("turl",URL);
            ajaxResult.put("message","uaencryptsc 效验不合法 返回首页URL");
            ajaxResult.put("tips","uaencryptsc 效验不合法 返回首页URL");
            ajaxResult.put("snsapi","NO");
            BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/ualogin","unifiedAuthentication","resp");
            return ajaxResult;
        }
        String uatoken = String.valueOf(map.get("uatoken"));
        if (ToolUtil.isEmpty(uatoken)){
            LOGGER.error("UaController.unifiedAuthentication().debug  错误为：uatoken 为空");
            ajaxResult.put("status",ErrorCodeConstants.USER_MOBLIE_ERROR_ERL);
            ajaxResult.put("turl",serviceCode.getTurl());
            ajaxResult.put("message","uatoken 为空,返回URL,不做登陆");
            ajaxResult.put("tips","uatoken 为空,返回URL,不做登陆");
            ajaxResult.put("snsapi","NO");
            BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/ualogin","unifiedAuthentication","resp");
            return ajaxResult;
        }
        String token = String.valueOf(map.get("token"));
        Object result = null ;
        if (ToolUtil.isEmpty(token)){
            result = uaService.guangdongMobileSso(map);
        }else {
            TMitUserToken userToken = (TMitUserToken) redisTemplate.opsForValue().get(token);
            if (ToolUtil.isNotEmpty(userToken)){
                result = uaService.verifiedJump(map);
            }else{
                EntityWrapper<TMitChannel> channelWrapper = new EntityWrapper<>();
                channelWrapper.eq("channelmark",serviceCode.getChannelmark());
                TMitChannel channel = channelService.selectOne(channelWrapper);
                Map resultMap = new HashMap();
                resultMap.put("status", ErrorCodeConstants.USER_WECHATPARAM_EXPIRED);
                resultMap.put("message", "用户登录超时！");
                resultMap.put("tips", "[2066]用户登录超时！");
                resultMap.put("snsapi",channel.getSnsapi());
                BlogUtil.inlog(JSON.toJSONString(resultMap),"","mit/user/ualogin","unifiedAuthentication","resp");
                return resultMap;
            }
        }
        BlogUtil.inlog(JSON.toJSONString(result),"","mit/user/ualogin","unifiedAuthentication","resp");
        return result;
	}
}
